package com.mcm.lstudy.dao.impl;

import java.util.List;

import com.mcm.lstudy.dao.CourseDao;
import com.mcm.lstudy.entity.Course;
import com.mcm.lstudy.tools.DBTools;
import com.mcm.lstudy.tools.PageData;

public class CourseDaoImpl implements CourseDao {

	@Override
	public boolean addCourse(Course course) {
		String sql = "insert into course values(null,?,?,?,?,?,?)";
		return DBTools.exUpdate(sql, course.getTypeid(), course.getCname(), course.getCteacher(), course.getCcontent(),
				course.getCprice(), course.getCstatus()) > 0;
	}

	@Override
	public boolean delCourse(int cid) {
		String sql = "update course set cstatus = 0 where cid = ?";
		return DBTools.exUpdate(sql, cid) > 0;
	}

	@Override
	public boolean activeCourse(int cid) {
		String sql = "update course set cstatus = 1 where cid = ?";
		return DBTools.exUpdate(sql, cid) > 0;
	}

	@Override
	public Course getCourseById(int cid) {
		String sql = "select * from course where cid = ?";
		List<Course> li = (List<Course>) DBTools.exQuery(sql, Course.class, cid);
		if (li.size() == 0) {
			return null;
		}
		return li.get(0);
	}

	@Override
	public boolean upadateCourse(Course course, int cid) {
		String sql = "update course set typeid=?,cname=?,cteacher=?,ccontent=?,cprice=?,cstatus=? where cid = ?";
		return DBTools.exUpdate(sql, course.getTypeid(), course.getCname(), course.getCteacher(), course.getCcontent(),
				course.getCprice(), course.getCstatus(), cid) > 0;
	}

	@Override
	public List<Course> getCourseByTypeId(int typeid) {
		String sql = "select * from course where typeid = ?";
		List<Course> li = (List<Course>) DBTools.exQuery(sql, Course.class, typeid);
		return li;
	}

	@Override
	public List<Course> getCourses(String keyword) {
		String sql = "select * from course where cname like ?";
		List<Course> li = (List<Course>) DBTools.exQuery(sql, Course.class, "%" + keyword + "%");
		return li;
	}

	@Override
	public PageData<Course> getCourseByPage(int page, int pageSize, String keyword) {
		String sql = "select * from course where cname like ?";
		return DBTools.exQueryByPage(sql, Course.class, page, pageSize, "%"+keyword+"%");
	}

}
